home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PsL Monthly 1993 December
/
PSL Monthly Shareware CD-ROM (December 1993).iso
/
prgmming
/
dos
/
c
/
gconio.exe
/
GCONIO.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1991-03-30
|
14KB
|
562 lines
/*
gconio.cpp
3-30-91
graphics console class for Borland C++
Copyright 1991
John W. Small
All rights reserved
Use freely but acknowledge authorship and copyright.
CIS: 73757,2233
PSW / Power SoftWare
P.O. Box 10072
McLean, Virginia 22102 8072
(703) 759-3838
*/
#include <gconio.hpp>
#include <stdio.h>
#include <stdarg.h>
#include <dos.h>
GraphicsConsole::GraphicsConsole()
{
ColumnLeft = 0;
InputWriteMode = COPY_PUT;
InputRewrite = 0;
InputDone = 0;
OutputWriteMode = COPY_PUT;
ClearTextBk = 0;
TextBkPattern = SOLID_FILL;
TextBkColor = 0; // 0 = current bkcolor
CursorChar[0] = '\124'; // 0 = no getch/gets cursor
CursorChar[1] = '\0';
CursorOnMsec = 100;
CursorOffMsec = 100;
}
void GraphicsConsole::ProportionalBackSpace(int& lastchX,
int& lastchY, int chX, int chY, char lastch,
char ch, struct textsettingstype& TS)
{
char lastchs[2];
char chs[2];
struct textsettingstype tmpTS;
lastchX = chX;
lastchY = chY;
lastchs[0] = lastch;
lastchs[1] = '\0';
chs[0] = ch;
chs[1] = '\0';
gettextsettings(&tmpTS);
settextstyle(TS.font,TS.direction,TS.charsize);
if (TS.direction == HORIZ_DIR)
switch (TS.horiz) {
case LEFT_TEXT:
lastchX -= textwidth(lastchs);
break;
case CENTER_TEXT:
lastchX -= (textwidth(chs) >> 1);
lastchX -= (textwidth(lastchs) >> 1);
break;
case RIGHT_TEXT:
lastchX -= textwidth(chs);
break;
}
else
switch (TS.vert) {
case BOTTOM_TEXT:
lastchY += textwidth(lastchs);
break;
case CENTER_TEXT:
lastchY += (textwidth(chs) >> 1);
lastchY += (textwidth(lastchs) >> 1);
break;
case TOP_TEXT:
lastchY += textwidth(chs);
break;
}
settextstyle(tmpTS.font,tmpTS.direction,tmpTS.charsize);
}
void GraphicsConsole::ProportionalSpace(int chX, int chY,
int& nextchX, int& nextchY, char ch, char nextch,
struct textsettingstype& TS)
{
char chs[2];
char nextchs[2];
struct textsettingstype tmpTS;
nextchX = chX;
nextchY = chY;
chs[0] = ch;
chs[1] = '\0';
nextchs[0] = nextch;
nextchs[1] = '\0';
gettextsettings(&tmpTS);
settextstyle(TS.font,TS.direction,TS.charsize);
if (TS.direction == HORIZ_DIR)
switch (TS.horiz) {
case LEFT_TEXT:
nextchX += textwidth(chs);
break;
case CENTER_TEXT:
nextchX += (textwidth(chs) >> 1);
nextchX += (textwidth(nextchs) >> 1);
break;
case RIGHT_TEXT:
nextchX += textwidth(nextchs);
break;
}
else
switch (TS.vert) {
case BOTTOM_TEXT:
nextchY -= textwidth(chs);
break;
case CENTER_TEXT:
nextchY -= (textwidth(chs) >> 1);
nextchY -= (textwidth(nextchs) >> 1);
break;
case TOP_TEXT:
nextchY -= textwidth(nextchs);
break;
}
settextstyle(tmpTS.font,tmpTS.direction,tmpTS.charsize);
}
void GraphicsConsole::TextBox(int x, int y, char *s,
struct textsettingstype& TS,
int& x1, int& y1, int& x2, int& y2)
{
int dx, dy;
struct textsettingstype tmpTS;
gettextsettings(&tmpTS);
settextstyle(TS.font,TS.direction,TS.charsize);
if (TS.direction == HORIZ_DIR) {
dx = textwidth(s);
dy = textheight(s);
// Stroked fonts have long decenders!
if (TS.font != DEFAULT_FONT)
y += (dy >> 2);
}
else {
dx = textheight(s);
dy = textwidth(s);
// Stroked fonts have long decenders!
if (TS.font != DEFAULT_FONT) {
x += (dx >> 2);
// Correct quirk in outtext[xy]
if (TS.horiz == CENTER_TEXT)
if ((dx % 2) != 0)
x += 2;
else
x++;
}
}
settextstyle(tmpTS.font,tmpTS.direction,tmpTS.charsize);
x1 = x; x2 = x1;
y1 = y; y2 = y1;
switch (TS.horiz) {
case LEFT_TEXT:
x2 += dx;
break;
case CENTER_TEXT:
x1 -= (dx >> 1);
x2 += (dx >> 1);
break;
case RIGHT_TEXT:
x1 -= dx;
break;
}
switch (TS.vert) {
case BOTTOM_TEXT:
y1 -= dy;
break;
case CENTER_TEXT:
y1 -= (dy >> 1);
y2 += (dy >> 1);
break;
case TOP_TEXT:
y2 += dy;
break;
}
}
int GraphicsConsole::cprintf(const char *format,...)
{
va_list argv;
int cnt;
char buf[GC_BUF_SIZE];
va_start(argv,format);
cnt = vsprintf(buf,format,argv);
va_end(argv);
if (cnt)
cputs(buf);
return cnt;
}
int GraphicsConsole::cputs(const char *str)
{
int i, j, x, y, x1, y1, x2, y2;
int bkcolor;
struct textsettingstype TS;
struct fillsettingstype FS;
char buf[GC_BUF_SIZE];
int ch;
if (!str)
return 0;
x = getx(); y = gety();
gettextsettings(&TS);
if (ClearTextBk) {
getfillsettings(&FS);
bkcolor = TextBkColor? TextBkColor : getbkcolor();
}
setwritemode(OutputWriteMode);
for (i = j = 0; j >= 0; i++) {
switch (str[i]) {
case '\r':
case '\n':
// fall thru and output buf
case '\0':
buf[j] = '\0';
if (ClearTextBk) {
setfillstyle(TextBkPattern,bkcolor);
TextBox(x,y,buf,TS,x1,y1,x2,y2);
bar(x1,y1,x2,y2);
setfillstyle(FS.pattern,FS.color);
}
outtextxy(x,y,buf);
if (TS.direction == HORIZ_DIR)
switch (str[i]) {
case '\r':
if (!ColumnLeft &&
(TS.horiz == LEFT_TEXT))
x = 0;
break;
case '\n':
y += 1 + textheight(buf);
break;
default:
if (TS.horiz == LEFT_TEXT)
x += textwidth(buf);
break;
}
else // TS.direction == VERT_DIR
switch (str[i]) {
case '\r':
if (!ColumnLeft &&
(TS.vert == BOTTOM_TEXT))
y = getmaxy();
break;
case '\n':
x += 1+textheight(buf);
break;
default:
if (TS.horiz == BOTTOM_TEXT)
y -= textwidth(buf);
break;
}
moveto(x,y);
j = (str[i])? 0 : -1;// terminate when ready
break;
default:
if (j < GC_BUF_SIZE - 1)
ch = buf[j++] = str[i];
break;
}
}
return ch;
}
int GraphicsConsole::putch(int c)
{
char chs[2];
chs[0] = c; chs[1] = '\0';
return cputs(chs);
}
void GraphicsConsole::rubout(int ch)
{
int x1, y1, x2, y2, lastchX, lastchY;
struct textsettingstype TS;
struct fillsettingstype FS;
char chs[2];
gettextsettings(&TS);
// Only works for left justified text!
if (TS.direction == HORIZ_DIR) {
if (TS.horiz != LEFT_TEXT)
return;
}
else if (TS.horiz != BOTTOM_TEXT)
return;
setwritemode(OutputWriteMode);
ProportionalBackSpace(lastchX,lastchY,
getx(),gety(),ch,ch,TS);
moveto(lastchX,lastchY);
chs[0] = ch;
chs[1] = '\0';
// Only erase XOR_PUT, stroked fonts!
if ((OutputWriteMode == COPY_PUT) ||
(TS.font == DEFAULT_FONT)) {
getfillsettings(&FS);
if (!TextBkColor)
setfillstyle(TextBkPattern,getbkcolor());
else
setfillstyle(TextBkPattern,TextBkColor);
TextBox(getx(),gety(),chs,TS,x1,y1,x2,y2);
bar(x1,y1,x2,y2);
setfillstyle(FS.pattern,FS.color);
}
else {
outtextxy(getx(),gety(),chs);
moveto(lastchX,lastchY);
}
}
int GraphicsConsole::cscanf(const char *format,...)
{
va_list argv;
int cnt;
char buf[GC_BUF_SIZE];
buf[0] = GC_BUF_SIZE - 2;
cgets(buf);
va_start(argv,format);
cnt = vsscanf(&buf[2],format,argv);
va_end(argv);
return cnt;
}
char * GraphicsConsole::cgets(char *str)
{
int i, imax, x, y, x1, y1, x2, y2;
int lastchX, lastchY, nextchX, nextchY;
struct textsettingstype TS;
struct fillsettingstype FS;
if (!str)
return str;
if ((imax = str[0]) < 2)
return (char *) 0;
x = getx(); y = gety();
gettextsettings(&TS);
setwritemode(InputWriteMode);
getfillsettings(&FS);
if (!TextBkColor)
setfillstyle(TextBkPattern,getbkcolor());
else
setfillstyle(TextBkPattern,TextBkColor);
InputDone = 0;
str[i = 2] = '\0';
while (!InputDone) {
// Cursor only for stroked fonts!
if (CursorChar[0] && (